package com.yugong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.common.Const;
import com.yugong.entity.dto.*;
import com.yugong.entity.vo.request.BusinessRequestVO;
import com.yugong.entity.vo.request.StudentRequestVO;
import com.yugong.entity.vo.response.BusinessListResponseVO;
import com.yugong.entity.vo.response.OrderClassListResponseVO;
import com.yugong.entity.vo.response.StudentListResponseVO;
import com.yugong.mapper.*;
import com.yugong.service.BusinessService;
import com.yugong.util.DigestUtils;
import com.yugong.util.TimeUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 企业业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/10
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private OrderClassMapper orderClassMapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private OrderClassAdminMapper orderClassAdminMapper;
    @Resource
    private MajorMapper majorMapper;
    @Resource
    private TimeUtils timeUtils;
    @Resource
    private DigestUtils digestUtils;

    /**
     * 查询企业信息
     *
     * @return 企业信息
     */
    @Override
    public List<BusinessListResponseVO> findBusiness() {
        LambdaQueryWrapper<Business> businessLambdaQueryWrapper = new LambdaQueryWrapper<>();
        businessLambdaQueryWrapper.ne(Business::getBusinessNo, "testBusiness");
        List<Business> businessList = businessMapper.selectList(businessLambdaQueryWrapper);
        List<BusinessListResponseVO> businessListResponseVOList = new ArrayList<>();
        for (Business business : businessList) {
            BusinessListResponseVO businessListResponseVO = new BusinessListResponseVO();
            businessListResponseVO.setBusinessNo(business.getBusinessNo());
            businessListResponseVO.setBusinessName(business.getBusinessName());
            if (business.getBusinessIntroduction() == null) {
                businessListResponseVO.setBusinessIntroduction("未填写");
            } else {
                businessListResponseVO.setBusinessIntroduction(business.getBusinessIntroduction());
            }
            businessListResponseVO.setBusinessAge(business.getBusinessAge());
            if (1 == business.getBusinessSex()) {
                businessListResponseVO.setBusinessSex("男");
            } else if (0 == business.getBusinessSex()) {
                businessListResponseVO.setBusinessSex("女");
            } else {
                businessListResponseVO.setBusinessSex("未填写");
            }
            businessListResponseVO.setBusinessOrderClassHeadName(business.getBusinessOrderClassHeadName());
            businessListResponseVO.setBusinessCreateMan(business.getBusinessCreateMan());
            businessListResponseVO.setBusinessRoomNo(business.getBusinessRoomNo());
            LambdaQueryWrapper<Room> roomLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLambdaQueryWrapper.eq(Room::getRoomNo, business.getBusinessRoomNo());
            Room room = roomMapper.selectOne(roomLambdaQueryWrapper);
            if (room == null) {
                businessListResponseVO.setBusinessRoomName("无");
            } else {
                businessListResponseVO.setBusinessRoomName(room.getRoomName());
            }
            if (business.getBusinessUpdateMan() == null) {
                businessListResponseVO.setBusinessUpdateMan("未更新");
            } else {
                businessListResponseVO.setBusinessUpdateMan(business.getBusinessUpdateMan());
            }
            String createTime = timeUtils.setDateToStrDate(business.getBusinessCreateTime(), Const.YYYY_MM_DD);
            businessListResponseVO.setBusinessCreateTime(createTime);
            if (business.getBusinessUpdateTime() == null) {
                businessListResponseVO.setBusinessUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(business.getBusinessUpdateTime(), Const.YYYY_MM_DD);
                businessListResponseVO.setBusinessUpdateTime(updateTime);
            }
            businessListResponseVOList.add(businessListResponseVO);
        }
        return businessListResponseVOList;
    }

    /**
     * 根据企业名查询企业信息
     *
     * @param pageNum      当前页
     * @param pageSize     当前页数据总数
     * @param businessName 企业名
     * @return 企业信息
     */
    @Override
    public PageInfo<BusinessListResponseVO> findBusiness(Integer pageNum, Integer pageSize, String businessName) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Business> businessLambdaQueryWrapper = new LambdaQueryWrapper<>();
        businessLambdaQueryWrapper
                .like(Business::getBusinessName, businessName)
                .orderByDesc(Business::getBusinessCreateTime);
        List<Business> businessList = businessMapper.selectList(businessLambdaQueryWrapper);
        List<BusinessListResponseVO> businessListResponseVOList = new ArrayList<>();
        QueryWrapper<Business> businessQueryWrapper = new QueryWrapper<>();
        businessQueryWrapper.like("business_name", businessName);
        Long businessCountNumber = businessMapper.selectCount(businessQueryWrapper);
        for (Business business : businessList) {
            BusinessListResponseVO businessListResponseVO = new BusinessListResponseVO();
            businessListResponseVO.setBusinessNo(business.getBusinessNo());
            if ("".equals(business.getBusinessName())) {
                businessListResponseVO.setBusinessName("未填写");
            } else {
                businessListResponseVO.setBusinessName(business.getBusinessName());
            }
            if (business.getBusinessIntroduction() == null) {
                businessListResponseVO.setBusinessIntroduction("未填写");
            } else {
                businessListResponseVO.setBusinessIntroduction(business.getBusinessIntroduction());
            }
            businessListResponseVO.setBusinessAge(business.getBusinessAge());
            if (1 == business.getBusinessSex()) {
                businessListResponseVO.setBusinessSex("男");
            } else if (0 == business.getBusinessSex()) {
                businessListResponseVO.setBusinessSex("女");
            } else {
                businessListResponseVO.setBusinessSex("未填写");
            }
            if ("".equals(business.getBusinessOrderClassHeadName())) {
                businessListResponseVO.setBusinessOrderClassHeadName("未填写");
            } else {
                businessListResponseVO.setBusinessOrderClassHeadName(business.getBusinessOrderClassHeadName());
            }
            Account account = accountMapper.selectById(business.getBusinessNo());
            if ("".equals(account.getAccountPhone())) {
                businessListResponseVO.setBusinessPhone("未填写");
            } else {
                businessListResponseVO.setBusinessPhone(digestUtils.base64Decode(account.getAccountPhone()));
            }
            if ("".equals(account.getAccountEmail())) {
                businessListResponseVO.setBusinessEmail("未填写");
            } else {
                businessListResponseVO.setBusinessEmail(digestUtils.base64Decode(account.getAccountEmail()));
            }
            businessListResponseVO.setBusinessCreateMan(business.getBusinessCreateMan());
            if (business.getBusinessRoomNo() == null || "".equals(business.getBusinessRoomNo())) {
                businessListResponseVO.setBusinessRoomNo("未填写");
                businessListResponseVO.setBusinessRoomName("未填写");
            } else {
                businessListResponseVO.setBusinessRoomNo(business.getBusinessRoomNo());
                Room room = roomMapper.selectById(business.getBusinessRoomNo());
                if (room == null) {
                    businessListResponseVO.setBusinessRoomName("未填写");
                } else {
                    businessListResponseVO.setBusinessRoomName(room.getRoomName());
                }
            }
            if (business.getBusinessUpdateMan() == null) {
                businessListResponseVO.setBusinessUpdateMan("未更新");
            } else {
                businessListResponseVO.setBusinessUpdateMan(business.getBusinessUpdateMan());
            }
            String createTime = timeUtils.setDateToStrDate(business.getBusinessCreateTime(), Const.YYYY_MM_DD);
            businessListResponseVO.setBusinessCreateTime(createTime);
            if (business.getBusinessUpdateTime() == null) {
                businessListResponseVO.setBusinessUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(business.getBusinessUpdateTime(), Const.YYYY_MM_DD);
                businessListResponseVO.setBusinessUpdateTime(updateTime);
            }
            businessListResponseVO.setBusinessCountNumber(businessCountNumber);
            businessListResponseVOList.add(businessListResponseVO);
        }
        return PageInfo.of(businessListResponseVOList);
    }

    /**
     * 修改企业
     *
     * @param businessRequestVO 企业请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int updateBusiness(BusinessRequestVO businessRequestVO) {
        Business business = new Business();
        business.setBusinessNo(businessRequestVO.getBusinessNo());
        business.setBusinessRoomNo(businessRequestVO.getBusinessRoomNo());
        business.setBusinessUpdateMan(businessRequestVO.getBusinessUpdateMan());
        business.setBusinessUpdateTime(new Date());
        return businessMapper.updateById(business);
    }


    /**
     * 删除企业
     *
     * @param businessNo 企业号
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteBusiness(String businessNo) {
        int deleteAccountFlag = accountMapper.deleteById(businessNo);
        int deleteBusinessFlag = businessMapper.deleteById(businessNo);
        return deleteAccountFlag + deleteBusinessFlag;
    }

    /**
     * 查询企业个人信息
     *
     * @param businessNo 企业号
     * @return 企业个人信息
     */
    @Override
    public BusinessListResponseVO businessInfo(String businessNo) {
        Business business = businessMapper.selectById(businessNo);
        BusinessListResponseVO businessListResponseVO = new BusinessListResponseVO();
        businessListResponseVO.setBusinessNo(businessNo);
        if (business == null) {
            businessListResponseVO.setBusinessName("");
            businessListResponseVO.setBusinessIntroduction("");
            businessListResponseVO.setBusinessOrderClassHeadName("");
            businessListResponseVO.setBusinessPhone("");
            businessListResponseVO.setBusinessEmail("");
            businessListResponseVO.setBusinessAge(null);
            businessListResponseVO.setBusinessSex("");
            businessListResponseVO.setBusinessRoomNo("");
            businessListResponseVO.setBusinessRoomName("");
        } else {
            if (business.getBusinessName() == null) {
                businessListResponseVO.setBusinessName("");
            } else {
                businessListResponseVO.setBusinessName(business.getBusinessName());
            }
            if (business.getBusinessIntroduction() == null) {
                businessListResponseVO.setBusinessIntroduction("");
            } else {
                businessListResponseVO.setBusinessIntroduction(business.getBusinessIntroduction());
            }
            if (business.getBusinessOrderClassHeadName() == null) {
                businessListResponseVO.setBusinessOrderClassHeadName("");
            } else {
                businessListResponseVO.setBusinessOrderClassHeadName(business.getBusinessOrderClassHeadName());
            }
            Account account = accountMapper.selectById(businessNo);
            if (account == null) {
                businessListResponseVO.setBusinessPhone("");
                businessListResponseVO.setBusinessEmail("");
            } else {
                if (account.getAccountPhone() == null) {
                    businessListResponseVO.setBusinessPhone("");
                } else {
                    businessListResponseVO.setBusinessPhone(digestUtils.base64Decode(account.getAccountPhone()));
                }
                if (account.getAccountEmail() == null) {
                    businessListResponseVO.setBusinessEmail("");
                } else {
                    businessListResponseVO.setBusinessEmail(digestUtils.base64Decode(account.getAccountEmail()));
                }
            }
            if (0 == business.getBusinessAge()) {
                businessListResponseVO.setBusinessAge(null);
            } else {
                businessListResponseVO.setBusinessAge(business.getBusinessAge());
            }
            if (1 == business.getBusinessSex()) {
                businessListResponseVO.setBusinessSex("男");
            } else if (0 == business.getBusinessSex()) {
                businessListResponseVO.setBusinessSex("女");
            } else {
                businessListResponseVO.setBusinessSex("");
            }
            Room room = roomMapper.selectById(business.getBusinessRoomNo());
            if (room == null) {
                businessListResponseVO.setBusinessRoomNo("");
                businessListResponseVO.setBusinessRoomName("");
            } else {
                if (room.getRoomNo() == null) {
                    businessListResponseVO.setBusinessRoomNo("");
                } else {
                    businessListResponseVO.setBusinessRoomNo(room.getRoomNo());
                }
                if (room.getRoomName() == null) {
                    businessListResponseVO.setBusinessRoomName("");
                } else {
                    businessListResponseVO.setBusinessRoomName(room.getRoomName());
                }
            }
        }
        return businessListResponseVO;
    }

    /**
     * 修改信息
     *
     * @param businessRequestVO 企业请求实体类
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateInfo(BusinessRequestVO businessRequestVO) {
        Date updateTime = new Date();
        Account account = new Account();
        account.setAccountNo(businessRequestVO.getBusinessNo());
        account.setAccountPhone(digestUtils.base64Encode(businessRequestVO.getBusinessPhone()));
        account.setAccountEmail(digestUtils.base64Encode(businessRequestVO.getBusinessEmail()));
        account.setAccountUpdateMan(businessRequestVO.getBusinessNo());
        account.setAccountUpdateTime(updateTime);
        int updateAccountFlag = accountMapper.updateById(account);
        Business business = new Business();
        business.setBusinessNo(businessRequestVO.getBusinessNo());
        business.setBusinessName(businessRequestVO.getBusinessName());
        business.setBusinessOrderClassHeadName(businessRequestVO.getBusinessOrderClassHeadName());
        business.setBusinessAge(businessRequestVO.getBusinessAge());
        business.setBusinessSex(businessRequestVO.getBusinessSex());
        business.setBusinessIntroduction(businessRequestVO.getBusinessIntroduction());
        business.setBusinessUpdateMan(businessRequestVO.getBusinessNo());
        business.setBusinessUpdateTime(updateTime);
        int updateBusinessFlag = businessMapper.updateById(business);
        return updateBusinessFlag + updateAccountFlag;
    }

    /**
     * 查看企业自己的学生评价
     *
     * @param pageNum     当前页
     * @param pageSize    当前页总数
     * @param studentName 姓名
     * @param businessNo  企业号
     * @return 企业自己的学生评价
     */
    @Override
    public PageInfo<StudentListResponseVO> findStudent(Integer pageNum, Integer pageSize, String studentName, String businessNo) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper
                .eq(Student::getStudentBusinessNo, businessNo)
                .like(Student::getStudentName, studentName);
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper
                .eq("student_business_no", businessNo)
                .like("student_name", studentName);
        Long studentCountNumber = studentMapper.selectCount(studentQueryWrapper);
        List<StudentListResponseVO> studentListResponseVOList = new ArrayList<>();
        List<Student> studentList = studentMapper.selectList(studentLambdaQueryWrapper);
        for (Student student : studentList) {
            StudentListResponseVO studentListResponseVO = new StudentListResponseVO();
            studentListResponseVO.setStudentNo(student.getStudentNo());
            studentListResponseVO.setStudentName(student.getStudentName());
            studentListResponseVO.setStudentOrderClassNo(student.getStudentOrderClassNo());
            studentListResponseVO.setStudentBusinessNo(student.getStudentBusinessNo());
            Business business = businessMapper.selectById(student.getStudentBusinessNo());
            OrderClass oc = orderClassMapper.selectById(student.getStudentOrderClassNo());
            if (business.getBusinessName() == null) {
                studentListResponseVO.setStudentBusinessName("无");
            } else {
                studentListResponseVO.setStudentBusinessName(business.getBusinessName());
            }
            if (oc.getOrderClassName() == null) {
                studentListResponseVO.setStudentOrderClassName("无");
            } else {
                studentListResponseVO.setStudentOrderClassName(oc.getOrderClassName());
            }
            if (student.getStudentEmploymentState() == 0) {
                studentListResponseVO.setStudentEmploymentState("未录用");
            } else {
                studentListResponseVO.setStudentEmploymentState("已录用");
            }
            studentListResponseVO.setStudentComprehensiveEvaluation(student.getStudentComprehensiveEvaluation());
            studentListResponseVO.setStudentCountNumber(studentCountNumber);
            studentListResponseVOList.add(studentListResponseVO);
        }
        return PageInfo.of(studentListResponseVOList);
    }

    /**
     * 企业录用
     *
     * @param studentRequestVO 学生请求实体类
     * @return 响应码
     */
    @Override
    public int employmentStudent(StudentRequestVO studentRequestVO) {
        Student student = new Student();
        student.setStudentNo(studentRequestVO.getStudentNo());
        student.setStudentEmploymentState(studentRequestVO.getStudentEmploymentState());
        student.setStudentUpdateMan(studentRequestVO.getStudentUpdateMan());
        student.setStudentUpdateTime(new Date());
        return studentMapper.updateById(student);
    }

    /**
     * 查询订单班
     *
     * @param pageNum        当前页
     * @param pageSize       当前页个数
     * @param orderClassName 班名
     * @param businessNo     企业号
     * @return 订单班
     */
    @Override
    public PageInfo<OrderClassListResponseVO> findOrderClass(Integer pageNum, Integer pageSize, String orderClassName, String businessNo) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<OrderClass> orderClassLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderClassLambdaQueryWrapper
                .eq(OrderClass::getOrderClassBusinessNo, businessNo)
                .like(OrderClass::getOrderClassName, orderClassName)
                .orderByDesc(OrderClass::getOrderClassCreateTime);
        List<OrderClass> orderClassList = orderClassMapper.selectList(orderClassLambdaQueryWrapper);
        QueryWrapper<OrderClass> orderClassQueryWrapper = new QueryWrapper<>();
        orderClassQueryWrapper
                .eq("order_class_business_no", businessNo)
                .like("order_class_name", orderClassName);
        Long orderClassCountNumber = orderClassMapper.selectCount(orderClassQueryWrapper);
        List<OrderClassListResponseVO> orderClassListResponseVOList = new ArrayList<>();
        for (OrderClass orderClass : orderClassList) {
            OrderClassListResponseVO orderClassListResponseVO = new OrderClassListResponseVO();
            orderClassListResponseVO.setOrderClassNo(orderClass.getOrderClassNo());
            orderClassListResponseVO.setOrderClassName(orderClass.getOrderClassName());
            orderClassListResponseVO.setOrderClassIntroduction(orderClass.getOrderClassIntroduction());
            if (0 == orderClass.getOrderClassState()) {
                orderClassListResponseVO.setOrderClassState(orderClass.getOrderClassState());
                orderClassListResponseVO.setOrderClassStateName("未发布");
            } else if (1 == orderClass.getOrderClassState()) {
                orderClassListResponseVO.setOrderClassState(orderClass.getOrderClassState());
                orderClassListResponseVO.setOrderClassStateName("未开班");
            } else if (2 == orderClass.getOrderClassState()) {
                orderClassListResponseVO.setOrderClassState(orderClass.getOrderClassState());
                orderClassListResponseVO.setOrderClassStateName("已开班");
            } else {
                orderClassListResponseVO.setOrderClassState(orderClass.getOrderClassState());
                orderClassListResponseVO.setOrderClassStateName("已结班");
            }
            orderClassListResponseVO.setOrderClassRealityNumber(orderClass.getOrderClassRealityNumber());
            String startTime = timeUtils.setDateToStrDate(orderClass.getOrderClassStartTime(), Const.YYYY_MM_DD);
            orderClassListResponseVO.setOrderClassStartTime(startTime);
            String EndTime = timeUtils.setDateToStrDate(orderClass.getOrderClassEndTime(), Const.YYYY_MM_DD);
            orderClassListResponseVO.setOrderClassEndTime(EndTime);
            orderClassListResponseVO.setOrderClassAdministratorNo(orderClass.getOrderClassAdministratorNo());
            OrderClassAdmin orderClassAdmin = orderClassAdminMapper.selectById(orderClass.getOrderClassAdministratorNo());
            if (orderClassAdmin == null) {
                orderClassListResponseVO.setOrderClassAdministratorName("未填写");
            } else {
                orderClassListResponseVO.setOrderClassAdministratorName(orderClassAdmin.getOrderClassAdminName());
            }
            orderClassListResponseVO.setOrderClassBusinessNo(orderClass.getOrderClassBusinessNo());
            Business business = businessMapper.selectById(orderClass.getOrderClassBusinessNo());
            if (business == null) {
                orderClassListResponseVO.setOrderClassBusinessName("未填写");
                orderClassListResponseVO.setOrderClassBusinessOrderClassHeadName("未填写");
            } else {
                orderClassListResponseVO.setOrderClassBusinessName(business.getBusinessName());
                orderClassListResponseVO.setOrderClassBusinessOrderClassHeadName(business.getBusinessOrderClassHeadName());
                orderClassListResponseVO.setOrderClassBusinessRoomNo(business.getBusinessRoomNo());
                Room room = roomMapper.selectById(business.getBusinessRoomNo());
                if (room == null) {
                    orderClassListResponseVO.setOrderClassBusinessRoomNo("未填写");
                    orderClassListResponseVO.setOrderClassBusinessRoomName("未填写");
                } else {
                    orderClassListResponseVO.setOrderClassBusinessRoomNo(room.getRoomNo());
                    orderClassListResponseVO.setOrderClassBusinessRoomName(room.getRoomName());
                }
            }
            orderClassListResponseVO.setOrderClassMajorNo(orderClass.getOrderClassMajorNo());
            Major major = majorMapper.selectById(orderClass.getOrderClassMajorNo());
            if (major == null) {
                orderClassListResponseVO.setOrderClassMajorName("未填写");
            } else {
                orderClassListResponseVO.setOrderClassMajorName(major.getMajorName());
            }
            orderClassListResponseVO.setOrderClassCreateMan(orderClass.getOrderClassCreateMan());
            String createTime = timeUtils.setDateToStrDate(orderClass.getOrderClassCreateTime(), Const.YYYY_MM_DD);
            orderClassListResponseVO.setOrderClassCreateTime(createTime);
            if (orderClass.getOrderClassCreateMan() == null) {
                orderClassListResponseVO.setOrderClassUpdateMan("未更新");
            } else {
                orderClassListResponseVO.setOrderClassUpdateMan(orderClass.getOrderClassCreateMan());
            }
            if (orderClass.getOrderClassUpdateTime() == null) {
                orderClassListResponseVO.setOrderClassUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(orderClass.getOrderClassUpdateTime(), Const.YYYY_MM_DD);
                orderClassListResponseVO.setOrderClassUpdateTime(updateTime);
            }
            orderClassListResponseVO.setOrderClassCountNumber(orderClassCountNumber);
            orderClassListResponseVOList.add(orderClassListResponseVO);
        }
        return PageInfo.of(orderClassListResponseVOList);
    }
}
